package com.od.service.serviceImpl;

import java.sql.Date;
import java.sql.Time;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.od.entity.Message;
import com.od.mapper.MessageMapper;
import com.od.service.MessageService;

@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageMapper messageMapper;

    @Override
    public List<Map<String, Object>> getMessagesByContact(String messageContact) {
        return messageMapper.selectMessagesByContact(Integer.parseInt(messageContact));
    }

    @Override
    public void sendMessage(Map<String, Object> newMessage) {
        Calendar calendar = Calendar.getInstance();
        Date currentDate = new Date(calendar.getTime().getTime());
        LocalTime now = LocalTime.now();
        Time currentTime = Time.valueOf(now);
        Message message = new Message(null, newMessage.get("messageContent").toString(),
                newMessage.get("messageType").toString(),
                currentDate, currentTime, Integer.parseInt(newMessage.get("messageContact").toString()));
        messageMapper.sendMessage(message);
    }
}
